﻿//-----------------------------------------------------------------
//
// @(#)$Id: ExpressAndWayOfDeliveryDetail.aspx.cs,v 1.0 2013/12/13 17:17:03 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:17:03 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal.CenterDB_BasTableAdapters;
using ebay.Dal.CenterDB_EbayTableAdapters;

public partial class UI_Bas_ExpressAndWayOfDeliveryDetail : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        ddl_c_Id.DataSource = new T_CountryCategoryTableAdapter().GetData();
        ddl_c_Id.DataTextField = "CountryCategory";
        ddl_c_Id.DataValueField = "Id";
        ddl_c_Id.DataBind();
    }
    static string act = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack )
        {
            act = Request.QueryString["act"];
            if(!String .IsNullOrEmpty (act))
            {
                switch (act)
                {
                    case "I":
                        break;
                    case "U":
                        {
                            this.txtId.Attributes["readonly"] = "readonly";
                            int id = int.Parse (Request.QueryString["id"]);
                            T_ExpressShippingMethodsTableAdapter adapter = new T_ExpressShippingMethodsTableAdapter ();
                            DataTable dt = adapter.GetDataById(id);
                            DataRow r=dt .Rows [0];
                            if (dt.Rows.Count > 0)
                            {
                                foreach (Control ctr in form1.Controls)
                                {
                                    if (ctr.ID == "ddl_c_Id")
                                    {
                                        ddl_c_Id.Value = r["CountryNames"].ToString();
                                        continue;
                                    }
                                    if (ctr is HtmlInputText)
                                    {
                                        HtmlInputText txtbox = (HtmlInputText)ctr;
                                        string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                        txtbox.Value = r[name].ToString();
                                    }
                                    else if (ctr is HtmlSelect)
                                    {
                                        HtmlSelect ddlist = (HtmlSelect)ctr;
                                        string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                        ddlist.Value = r[name].ToString();
                                    }
                                    else if (ctr is DropDownList )
                                    {
                                        DropDownList ddlist = (DropDownList)ctr;
                                        string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                        ddlist.Text = r[name].ToString();
                                    }
                                }
                            }
                        }
                        break;                    
                }               
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
      
        if (!String.IsNullOrEmpty(act))
        {
            switch (act)
            {
                case "I":
                    {
                        T_ExpressShippingMethodsTableAdapter adapter = new T_ExpressShippingMethodsTableAdapter();
                        int flag = (int)adapter.IsExists(int.Parse(txtId.Value));
                        if (flag == 0)
                        {
                            int val = adapter.Insert(
                                int.Parse(txtId.Value), txtExpress.Value, txtEbayShippingMethods.Value, ddl_c_Id.Value,
                                Convert.ToDecimal(txtStartingWeight.Value), Convert.ToDecimal(txtEndingWeight.Value), Convert.ToDecimal(txtStartingLength.Value),
                                Convert.ToDecimal(txtEndingLength.Value), Convert.ToDecimal(txtStartingWidth.Value), Convert.ToDecimal(txtEndingWidth.Value),
                                Convert.ToDecimal(txtStartingHeight.Value), Convert.ToDecimal(txtEndingHeight.Value), Convert.ToDecimal(txtStartingAmount.Value),
                                Convert.ToDecimal(txtEndingAmount.Value), txtDelivery.Value, txtDeliveryCode.Value, Convert.ToInt32(ddlPictureId.Value));
                            if (val > 0)
                            {
                                Response.Write("<script>alert('添加成功！');window.location.href='ExpressAndWayOfDelivery.aspx'</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('编号重复!...');</script>");
                        }
                    }
                    break;
                case "U":
                    {
                        T_ExpressShippingMethodsTableAdapter adapter = new T_ExpressShippingMethodsTableAdapter();
                        DataTable dt = adapter.GetDataById(int.Parse(txtId.Value) );
                        DataRow r = dt.Rows[0];
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control ctr in form1.Controls)
                            {

                                if (ctr.ID == "ddl_c_Id")
                                {
                                    r["CountryNames"] = ddl_c_Id.Value;
                                    continue;
                                }
                                if (ctr is HtmlInputText)
                                {
                                    HtmlInputText txtbox = (HtmlInputText)ctr;
                                    string name = txtbox.ID.Substring(3, txtbox.ID.Length - 3);                                                                      
                                    r[name] = txtbox.Value;
                                }
                                else if (ctr is HtmlSelect)
                                {
                                    HtmlSelect ddlist = (HtmlSelect)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);

                                    r[name] = ddlist.Value;
                                }
                                else if (ctr is DropDownList)
                                {
                                    DropDownList ddlist = (DropDownList)ctr;
                                    string name = ddlist.ID.Substring(3, ddlist.ID.Length - 3);
                                    r[name] = ddlist.SelectedValue;
                                }
                            }
                        }
                        int val = adapter.Update(r);                       
                        if (val > 0)
                        {
                            Response.Write("<script>alert('修改成功！');window.location.href='ExpressAndWayOfDelivery.aspx'</script>");
                        }              
                    }
                    break;
            }
        }
    }
}
